﻿using System;
using System.IO;
using System.Windows.Forms;

namespace SwimmingCompetitions
{
    internal class Logger : IDisposable
    {
        private readonly StreamWriter _stream;

        private Logger(string fileName)
        {
            _stream = new StreamWriter(fileName);
            _stream.AutoFlush = true;
        }

        private static Logger _instance;

        public static Logger Instance
        {
            get
            {
                return _instance ??
                       (_instance =
                        new Logger(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "log.txt")));
            }
        }

        public void Log(string message)
        {
            _stream.WriteLine("{0}: {1}", DateTime.Now, message);
        }

        public void Log(string message, Exception ex)
        {
            _stream.WriteLine("{0}: {1}; Exception: {2}", DateTime.Now, message, ex);
        }

        public void Dispose()
        {
            if (_stream != null) _stream.Close();
        }
    }
}
